﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace CommonUtil
{
    /// <summary>
    /// 临时汇总表操作类
    /// </summary>
    public static class TmpSummary
    {
        private static int isSave;
        /// <summary>
        /// 是否临时保存
        /// </summary>
        public static int IsSave
        {
            get { return TmpSummary.isSave; }
            set { TmpSummary.isSave = value; }
        }

        /// <summary>
        /// 生成临时汇总表的DataTable结构
        /// </summary>
        /// <returns></returns>
        public static DataTable CreateDataTable()
        {
            //创建结构
            DataTable dt = new DataTable();
            //DataColumn dcId = new DataColumn("Id", Type.GetType("System.Int32"));
            DataColumn dcId = new DataColumn("Id", Type.GetType("System.String"));
            DataColumn dcTmpId = new DataColumn("TmpId", Type.GetType("System.String"));
            DataColumn dcSerialId = new DataColumn("SerialId", Type.GetType("System.String"));
            DataColumn dcTmpType = new DataColumn("TmpType", Type.GetType("System.String"));
            DataColumn dcEnName = new DataColumn("EnName", Type.GetType("System.String"));
            DataColumn dcEnValue = new DataColumn("EnValue", Type.GetType("System.String"));
            DataColumn dcIsSave = new DataColumn("IsSave", Type.GetType("System.Int32"));
            //dcId.AutoIncrement = true;  //设置是否为自增列
            //dcId.AutoIncrementSeed = 1;   //设置自增初始值
            //dcId.AutoIncrementStep = 1;   //设置每次子增值
            dt.Columns.Add(dcId);            
            dt.Columns.Add(dcTmpId);
            dt.Columns.Add(dcSerialId);
            dt.Columns.Add(dcTmpType);
            dt.Columns.Add(dcEnName);
            dt.Columns.Add(dcEnValue);
            dt.Columns.Add(dcIsSave);
            dt.PrimaryKey = new DataColumn[] { dcId };
            //dt.RowChanged += new DataRowChangeEventHandler(dt_RowChanged);
            return dt;
        }

        //static void dt_RowChanged(object sender, DataRowChangeEventArgs e)
        //{
            
        //}
        /// <summary>
        /// 写入或更新数据到临时汇总表的DataTable
        /// </summary>
        public static void WriteDataTable(TmpSummaryObj tmpSumObj, DataTable dt)
        {
            if (dt != null)
            {
                if (dt.Rows.Count == 0)
                {
                    AddRowToDataTable(tmpSumObj, dt);
                }
                else
                {
                    DataRow[] drArray = dt.Select(string.Format("TmpId='{0}' and EnName='{1}'", tmpSumObj.TmpId, tmpSumObj.EnName));
                    if (drArray != null && drArray.Length != 0)
                    {
                        drArray[0]["EnValue"] = tmpSumObj.EnValue;
                    }
                    else
                    {
                        AddRowToDataTable(tmpSumObj, dt);
                    }
                }
            }
        }       
        /// <summary>
        /// 增加记录到临时汇总表的DataTable
        /// </summary>
        /// <param name="tmpSumObj"></param>
        public static void AddRowToDataTable(TmpSummaryObj tmpSumObj, DataTable dt)
        {
            DataRow drNew = dt.NewRow();
            drNew["Id"] = Guid.NewGuid().ToString();
            drNew["TmpId"] = tmpSumObj.TmpId;
            drNew["SerialId"] = tmpSumObj.SerialId;
            drNew["TmpType"] = tmpSumObj.TmpType;
            drNew["EnName"] = tmpSumObj.EnName;
            drNew["EnValue"] = tmpSumObj.EnValue;
            drNew["IsSave"] = tmpSumObj.IsSave;
            dt.Rows.Add(drNew);
        }
        /// <summary>
        /// 将DataTable写入到对应的表
        /// </summary>
        public static void DataTableToDB(string connString, DataTable dt)
        {
            try
            {
                SqlHelper.ConnectionString(connString);
                SqlHelper.Datatabletosqlserver(dt, "TmpSummary");
            }
            catch (Exception ex)
            {
                string err = ex.Message + "\r\n";
                Logger.DefaultLog(err + ex.StackTrace, LogType.Error);
                StringBuilder sb = new StringBuilder();
                if(dt!=null && dt.Rows.Count>0)
                {
                    for(int i=0; i<dt.Columns.Count; i++)
                    {
                        sb.Append( " | " +dt.Rows[0][i].ToString());
                    }
                }
                Logger.DefaultLog("Datatabletosqlserver(TmpSummary)" + sb.ToString(), LogType.Information);
            }
        }
    }
    
}
